import json
from django.shortcuts import HttpResponse
from service.company_node_llm_service import CompanyNodeLLMService

#查询节点
def query_llm_company_node(request):
    if request.method == 'GET':
        company_node_llm_service = CompanyNodeLLMService()
        rs=company_node_llm_service.query_node()
        if len(rs)>0:
            data={"code":200,"msg":"查询成功","data":rs}
        else:
            data={"code":500,"msg":"没查询到数据","data":None}
        return HttpResponse(json.dumps(data))

#创建节点
def create_llm_company_node(request):
    if request.method == 'GET':
        company_node_llm_service = CompanyNodeLLMService()
        #获取前端发送的请求
        reg_date=request.GET.get('reg_date')
        risk_level=request.GET.get('risk_level')
        reg_no=request.GET.get('reg_no')
        name=request.GET.get('name')
        industry=request.GET.get('industry')
        legal_rep=request.GET.get('legal_rep')
        status=request.GET.get('status')
        try:
            company_node_llm_service.create_node(
                reg_date=reg_date,
                risk_level=risk_level,
                reg_no=reg_no,
                name=name,
                industry=industry,
                legal_rep=legal_rep,
                status=status
            )
            data={"code":200,"msg":"创建成功","data":None}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data={"code":500,"msg":"创建失败","data":None}
            return HttpResponse(json.dumps(data))

#更新节点
def update_llm_company_node(request):
    if request.method == 'GET':
        company_node_llm_service = CompanyNodeLLMService()
        # 获取前端发送的请求
        reg_date = request.GET.get('reg_date')
        risk_level = request.GET.get('risk_level')
        reg_no = request.GET.get('reg_no')
        name = request.GET.get('name')
        industry = request.GET.get('industry')
        legal_rep = request.GET.get('legal_rep')
        status = request.GET.get('status')
        try:
            company_node_llm_service.update_node(
                reg_date=reg_date,
                risk_level=risk_level,
                reg_no=reg_no,
                name=name,
                industry=industry,
                legal_rep=legal_rep,
                status=status
            )
            data = {"code": 200, "msg": "更新成功", "data": None}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data = {"code": 500, "msg": "更新失败", "data": None}
            return HttpResponse(json.dumps(data))

#删除节点
def delete_llm_company_node(request):
    if request.method == 'GET':
        company_node_llm_service = CompanyNodeLLMService()
        name=request.GET.get('name')
        try:
            company_node_llm_service.delete_node(name=name)
            data={"code":200,"msg":"删除成功","data":None}
            return HttpResponse(json.dumps(data))
        except Exception as e:
            print(e)
            data = {"code": 500, "msg": "删除失败", "data": None}
            return HttpResponse(json.dumps(data))